package features.advance.leetcode.math.easy;

/**
 * @author LIN
 * @date 2022-03-07 16:17
 *
 *  504. 七进制数
 *
 *  难度：简单
 *
 * 给定一个整数 num，将其转化为 7 进制，并以字符串形式输出。
 *
 *
 *
 * 示例 1:
 *
 * 输入: num = 100
 * 输出: "202"
 * 示例 2:
 *
 * 输入: num = -7
 * 输出: "-10"
 *
 *
 * 提示：
 *
 * -107 <= num <= 107
 */
public class Solution504 {

    public static void main(String[] args) {
        Solution solution = new Solution() {
        };
        int num = 50;
        String s = solution.convertToBase7(num);
        System.out.println(s);

    }

    static class Solution {
        public String convertToBase7(int num) {
            StringBuilder sb = new StringBuilder();
            boolean isN = false;
            if(num < 0){
                num*=-1;
                isN = true;
            }else if(num == 0){
                return "0";
            }
            int a = num % 7;
            int b = num / 7;
            sb.append(a);
            while(b > 0){
                a = b%7;
                b/=7;
                sb.append(a);
            }
            if(isN){
                sb.append("-");
            }
            return sb.reverse().toString();
        }
    }
}
